/*******************************************************************************
* CSCI321 Major Project 2010 - University of Wollongong                        *
********************************************************************************
* Program:		WeaveEdit                                              *
* Authors:		Simon Allen (Student#: 3457527, Unix: sa655)           *
*			Scott Bellis (Student#: 3479110, Unix: sjb900)         *
*			Jason Benger (Student#: 3454137, Unix: jb960)          *
*			Timothy Newmarch (Student#: 3459263 Unix: tgn336)      *
*			Benjamin Richardson (Student# 3467752, Unix: bjr401)   *
* Supervisor:		Daniel F. Saffioti                                     *
* Modified:		28/10/2010                                             *
* Description:		WeaveEdit is a highly portable collaborative text      *
*			editor and word processor, allowing several users to   *
*			read and edit multiple documents at the same time.     *
*			It is available for Mac OS X, Windows and Linux,       *
*			utilising Nokia's Qt development framework.            *
*******************************************************************************/


#include <QTextCursor>
#include <QTextDocument>
#include <QMessageBox>
#include "userhighlighter.h"

UserHighlighter::UserHighlighter(QTextDocument *newParent, UserManager *newUserManager)
	: srchiliteqt::Qt4SyntaxHighlighter(newParent)
{
	highlighted = false;
	userManager = newUserManager;
	parent = newParent;
	syntaxHighlighted = false;
}

void UserHighlighter::highlightBlock(const QString &text)
{

    if (syntaxHighlighted)
	srchiliteqt::Qt4SyntaxHighlighter::highlightBlock(text);

    if (highlighted)
    {
		//we need to save the previous signal state, since signals may already be blocked from
		//calling rehighlight() in thh toggleHighlighting function, and apparently rehighlight does
		//more than just call this function
	    bool previousSignalState = document()->blockSignals(true);

	    int length = currentBlock().length();
	    QTextCursor selection(document());

	    selection.setPosition(currentBlock().position() + 1);

	    for (int i = 0; i < length; i++)
	    {
		int currentUser = selection.charFormat().property(QTextFormat::UserProperty).toInt() - 1;

		/*QMessageBox msg;
		msg.setText(QString::number(currentUser));
		msg.exec();*/

		    //set the format based on the user property
		    QTextCharFormat fmt = format(i);

		    if (currentUser != -1)
		    {
			    QColor back = userManager->getUserFromId(currentUser).getColour();
			    fmt.setBackground(QBrush(back));

			    //set foreground colour based on the background one
			    QColor fore;

			    if (syntaxHighlighted)
				    fore = fmt.foreground().color();
			    else
				    fore = selection.charFormat().foreground().color();

			    int h,s,l;
			    fore.getHsl(&h,&s,&l);

			    int difference = back.lightness() - l;

			    //if the difference in colours is insignificant make the contrast increased
			    if ((difference >= 0 && difference < 50) || (difference > -50 && difference < 0))
					    l = (l + 128) % 256;

			    fore.setHsl(h,s,l);

			    fmt.setForeground(QBrush(fore));

			    setFormat(i, 1, fmt);
		    }

		    selection.movePosition(QTextCursor::NextCharacter);
	    }

			document()->blockSignals(previousSignalState);
    }
}

void UserHighlighter::toggleHighlighting(const bool mode)
{
	highlighted = mode;
	rehighlight();
}

void UserHighlighter::toggleSyntaxHighlighting(const bool mode)
{
	syntaxHighlighted = mode;
	rehighlight();
}

void UserHighlighter::rehighlight()
{
	bool previousState = document()->signalsBlocked();

	document()->blockSignals(true);
	srchiliteqt::Qt4SyntaxHighlighter::rehighlight();
	document()->blockSignals(previousState);
}

